package UILayer;

import java.util.Scanner;
import CtrlLayer.*;
import ModelLayer.*;
/**
 * Write a description of class UnregisteredCustomerUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UnregisteredCustomerUI
{
    private CustomerController customerContainer;
    private ProductController productContainer;
    

    public UnregisteredCustomerUI()
    {
        customerContainer = new CustomerController();
        productContainer = new ProductController();
    }

     public void unregisteredCustomerMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeUnregisteredCustomerMenu();

            switch (choise) 
            {
                case 1: startCreateCustomer();
                break;
                case 2: startOrderProduct();
                break;
                case 3: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writeUnregisteredCustomerMenu()
    {  
        int choise = 0;
        try
        {
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Employee Menu ***");
            System.out.println(" *** Speaking with unregistered customer ***");
            System.out.println(" (1) Create customer");
            System.out.println(" (2) Proceed ot ordering without registration");
            System.out.println(" (3) Back");

            choise = keyboard.nextInt();
        }
        catch(Exception e)
        {
            System.out.println("ERROR!");
            unregisteredCustomerMenuStart();
        }
        return choise;
    }
    
    private void startCreateCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        try 
        {
            System.out.println("Fill in the information of the new customer: ");
            System.out.println("ID:");
            int customerId = Integer.parseInt(keyboard.nextLine());
            System.out.println("Customer's Name:");
            String name = keyboard.nextLine();
            System.out.println("Customer's Address:");
            String address = keyboard.nextLine();
            System.out.println("Customer's City:");
            String city = keyboard.nextLine();
            System.out.println("Customer's Post Code:");
            String postCode = keyboard.nextLine();
            System.out.println("Customer's e-mail:");
            String eMail = keyboard.nextLine();

            Customer customer = new Customer(customerId, name, address, city, postCode, eMail);
            if(customerContainer.addCustomer(customer))
            {
                customerContainer.addCustomer(customer);
                System.out.println("A new customer has been added!");
            } 
            else 
            {
                System.out.println("A customer with the same id already exists!");
            }
        } 
        catch (Exception e) 
        {
            System.out.println("Something went wrong!");
        }
        pause();
    }
    
    private void startOrderProduct()
    {
        int index=0;
        boolean exit=false;
        while(exit==false)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the name of the product you want to buy:");
            try
            {
                String name = keyboard.nextLine();
                Product foundProduct = productContainer.findProductByName(name);
                System.out.println("Barcode: " + foundProduct.getBarCode());
                System.out.println("Product's name: " + foundProduct.getProductName());
                System.out.println("Price: " + foundProduct.getPrice());
                System.out.println("Weight: " + foundProduct.getWeight());
                System.out.println("Location: " + foundProduct.getLocation());
                System.out.println("Quantity available: " + foundProduct.getQuantity());
                System.out.println("============================================");
                System.out.println("How much of this product would you like to buy?");
                int quantity = keyboard.nextInt();
                foundProduct.setQuantity(foundProduct.getQuantity() - quantity);
                System.out.println("Would you like us to deliver it?");
                //===========================DA SE DOPISHE TUK=============================================
                System.out.println("Would you like to order more products?");
                System.out.println("       (1) Yes");
                System.out.println("       (2) No");
                int choise = keyboard.nextInt();
                if(choise==1)
                {
                    exit=false;
                }
                else
                {
                    exit = true;
                }
            }

            catch (Exception e) 
            {
                System.out.println("The product you are looking for can not be found!");
            }
            pause();
        }
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
